/** 已绑定设备列表 */
import React, { Component } from 'react';
import { Link } from 'dva/router';
import { Table } from 'antd';
import { isSuccess } from '../../configs/error';
import device from '../../services/device';
import { getColumns } from '../../components/export';

const gender = ['未知', '男', '女'];

const userColumns = [
	{
		title: '用户ID',
		dataIndex: 'userId',
		key: 'userId'
	},
	{
		title: 'openID',
		dataIndex: 'openid',
		key: 'openid'
	},
	{
		title: '呢称',
		dataIndex: 'nickname',
		key: 'nickname'
	},
	{
		title: '头像',
		dataIndex: 'avatar',
		key: 'x',
		render: (text) => {
			return (
				<span>
					<img style={{ width: '30px', verticalAlign: 'bottom' }} src={text} />
				</span>
			);
		},
		width: 120
	},
	{
		title: '联系方式',
		dataIndex: 'mobile',
		key: 'mobile',
	},
	{
		title: '邮箱',
		dataIndex: 'email',
		key: 'email'
	},
	{
		title: '性别',
		dataIndex: 'gender',
		key: 'gender',
		render: g => gender[g]
	},
	{
		title: '所在地',
		dataIndex: 'region',
		key: 'region'
	},
	{
		title: '是否管理员',
		dataIndex: 'isManage',
		key: 'isManage',
		render: isManage => isManage ? '是' : '否'
	},
	{
		title: '绑定时间',
		dataIndex: 'bindTime',
		key: 'bindTime'
	},
	{
		title: '注册时间',
		dataIndex: 'registerTime',
		key: 'registerTime'
	},
	{
		title: '授权时间',
		dataIndex: 'authorizationTime',
		key: 'authorizationTime'
	},
	{
		title: '最后登录时间',
		dataIndex: 'lastLoginTime',
		key: 'lastLoginTime'
	},
	{
		title: '操作',
		key: 'opt',
		render(text, record) {
			return <Link to={`/wxList/${record.id}`}>详情</Link>;
		}
	}
];

class BindUserList extends Component {
	state = {
		data: [],
		display: []
	}

	componentWillMount() {
		this.getData();
	}

	componentWillReceiveProps(nextProps) {
		if (nextProps.sno !== this.props.sno) {
			this.getData();
		}
	}

	async getData() {
		const { sno } = this.props;
		if (!sno) return;
		const res = await device.getDeviceBindUserList({
			query: { sno },
			size: 20,
			current: 1
		});
		if (isSuccess(res)) {
			this.setState({ data: res.data.records, display: res.display });
		}
	}

	render() {
		const { sno, ...props } = this.props;
		const { data, display } = this.state;
		return (
			<div>
				<Table
					dataSource={data}
					columns={getColumns(userColumns, display)}
					{...props}
					pagination={false}
					rowKey="id"
				/>
			</div>
		);
	}
}

export default BindUserList;
